package main.java.learning.directory.Work.数组.Ac;

/**
 * @author aichao
 */
public class BinarySearchDay5 {
    public static void main(String[] args) {
        // int arr[] = {1, 3, 5, 7, 9, 11};
        int[] arr = {49, 38, 65, 97, 23, 22, 76, 1, 50, 90, 31, 89, 31};
        System.out.println(binarySerach(arr, 50));
    }

    public static int binarySerach(int[] array, int key) {
        int left = 0;
        int right = array.length;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (array[mid] == key) {
                return mid;
            } else if (array[mid] < key) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }
}
